// Copyright (c) 2025, 大连理工大学 (Dalian University of Technology)
//
// Licensed under the Mulan PSL v2.
// You can use this file according to the terms and conditions of the Mulan PSL v2.
// You may obtain a copy of the License at
//
//     http://license.coscl.org.cn/MulanPSL2
//
// THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
// EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. SEE THE MULAN PSL v2
// FOR MORE DETAILS.

#include "Import/BDFImport/BulkData/Property/PSOLIDParser.h"

#include "gtest/gtest.h"

#include "DataStructure/Common/Constant.h"

#include "DBManager/DBServiceFactory.h"
#include "DBManager/IService/Input/Property/IPSOLIDService.h"

using SG::DataStructure::Common::TOLERANCE;
using namespace SG::DataStructure::FEM;
#include "Import/BDFImport/BDFParserBase.h"
using namespace SG::DBManager;
using namespace SG::Import;
extern std::shared_ptr<DBServiceFactory> DB_SERVICEFACTORY;
extern std::vector<std::string>          dividedEntryInfo;

class PSOLIDParserTest : public ::testing::Test
{
  public:
    static void SetUpTestSuite ()
    {
        DB_SERVICEFACTORY->ClearDatabase ();
    }
};

TEST_F (PSOLIDParserTest, ParseEntry1)
{
    std::vector<std::string> Entry = { "PSOLID  13245021 457     0" };
    BDF::divideBulkEntry (Entry, dividedEntryInfo);
    SG::Import::BDF::BULKDATA::ParsePSOLID (DB_SERVICEFACTORY, dividedEntryInfo);
    SG::DataStructure::FEM::PSOLIDData data;

    ASSERT_TRUE (DB_SERVICEFACTORY->get<IPSOLIDService> ()->FindById (13245021, data) == SG::DataStructure::Common::Status::STATUS_Success);

    ASSERT_TRUE (data.m_id == 13245021);
    ASSERT_TRUE (data.m_mId == 457);
    ASSERT_TRUE (data.m_mCId == 0);
}

TEST_F (PSOLIDParserTest, ParseEntry2)
{
    std::vector<std::string> Entry = { "PSOLID         2       1               1   GAUSS REDUCEDSMECH" };
    BDF::divideBulkEntry (Entry, dividedEntryInfo);
    SG::Import::BDF::BULKDATA::ParsePSOLID (DB_SERVICEFACTORY, dividedEntryInfo);
    SG::DataStructure::FEM::PSOLIDData data;

    ASSERT_TRUE (DB_SERVICEFACTORY->get<IPSOLIDService> ()->FindById (2, data) == SG::DataStructure::Common::Status::STATUS_Success);

    ASSERT_TRUE (data.m_id == 2);
    ASSERT_TRUE (data.m_mId == 1);
    ASSERT_TRUE (data.m_mCId == 0);
    ASSERT_TRUE (data.m_in == 1);
    ASSERT_TRUE (data.m_stress == 1);
    ASSERT_TRUE (data.m_iosp == 0);
}

TEST_F (PSOLIDParserTest, ParseEntry3)
{
    std::vector<std::string> Entry = { "PSOLID         3       1               2    GRID    FULLSMECH" };
    BDF::divideBulkEntry (Entry, dividedEntryInfo);
    SG::Import::BDF::BULKDATA::ParsePSOLID (DB_SERVICEFACTORY, dividedEntryInfo);
    SG::DataStructure::FEM::PSOLIDData data;

    ASSERT_TRUE (DB_SERVICEFACTORY->get<IPSOLIDService> ()->FindById (3, data) == SG::DataStructure::Common::Status::STATUS_Success);

    ASSERT_TRUE (data.m_id == 3);
    ASSERT_TRUE (data.m_mId == 1);
    ASSERT_TRUE (data.m_mCId == 0);
    ASSERT_TRUE (data.m_in == 2);
    ASSERT_TRUE (data.m_stress == 0);
    ASSERT_TRUE (data.m_iosp == 1);
}

TEST_F (PSOLIDParserTest, ParseEntry4)
{
    std::vector<std::string> Entry = { "PSOLID         4       1               3                SMECH" };
    BDF::divideBulkEntry (Entry, dividedEntryInfo);
    SG::Import::BDF::BULKDATA::ParsePSOLID (DB_SERVICEFACTORY, dividedEntryInfo);
    SG::DataStructure::FEM::PSOLIDData data;

    ASSERT_TRUE (DB_SERVICEFACTORY->get<IPSOLIDService> ()->FindById (4, data) == SG::DataStructure::Common::Status::STATUS_Success);

    ASSERT_TRUE (data.m_id == 4);
    ASSERT_TRUE (data.m_mId == 1);
    ASSERT_TRUE (data.m_mCId == 0);
    ASSERT_TRUE (data.m_in == 3);
    ASSERT_TRUE (data.m_stress == 0);
    ASSERT_TRUE (data.m_iosp == 0);
}

TEST_F (PSOLIDParserTest, ParseEntry5)
{
    std::vector<std::string> Entry = { "PSOLID         5       1               0                SMECH" };
    BDF::divideBulkEntry (Entry, dividedEntryInfo);
    SG::Import::BDF::BULKDATA::ParsePSOLID (DB_SERVICEFACTORY, dividedEntryInfo);
    SG::DataStructure::FEM::PSOLIDData data;

    ASSERT_TRUE (DB_SERVICEFACTORY->get<IPSOLIDService> ()->FindById (5, data) == SG::DataStructure::Common::Status::STATUS_Success);

    ASSERT_TRUE (data.m_id == 5);
    ASSERT_TRUE (data.m_mId == 1);
    ASSERT_TRUE (data.m_mCId == 0);
    ASSERT_TRUE (data.m_in == 0);
    ASSERT_TRUE (data.m_stress == 0);
    ASSERT_TRUE (data.m_iosp == 0);
}

TEST_F (PSOLIDParserTest, ParseEntry6)
{
    std::vector<std::string> Entry = { "PSOLID         6       1           THREE       1       1SMECH" };
    BDF::divideBulkEntry (Entry, dividedEntryInfo);
    SG::Import::BDF::BULKDATA::ParsePSOLID (DB_SERVICEFACTORY, dividedEntryInfo);
    SG::DataStructure::FEM::PSOLIDData data;

    ASSERT_TRUE (DB_SERVICEFACTORY->get<IPSOLIDService> ()->FindById (6, data) == SG::DataStructure::Common::Status::STATUS_Success);

    ASSERT_TRUE (data.m_id == 6);
    ASSERT_TRUE (data.m_mId == 1);
    ASSERT_TRUE (data.m_mCId == 0);
    ASSERT_TRUE (data.m_in == 3);
    ASSERT_TRUE (data.m_stress == 1);
    ASSERT_TRUE (data.m_iosp == 1);
}

TEST_F (PSOLIDParserTest, ParseEntry7)
{
    std::vector<std::string> Entry = { "PSOLID         7       1             TWO       1       1SMECH" };
    BDF::divideBulkEntry (Entry, dividedEntryInfo);
    SG::Import::BDF::BULKDATA::ParsePSOLID (DB_SERVICEFACTORY, dividedEntryInfo);
    SG::DataStructure::FEM::PSOLIDData data;

    ASSERT_TRUE (DB_SERVICEFACTORY->get<IPSOLIDService> ()->FindById (7, data) == SG::DataStructure::Common::Status::STATUS_Success);

    ASSERT_TRUE (data.m_id == 7);
    ASSERT_TRUE (data.m_mId == 1);
    ASSERT_TRUE (data.m_mCId == 0);
    ASSERT_TRUE (data.m_in == 2);
    ASSERT_TRUE (data.m_stress == 1);
    ASSERT_TRUE (data.m_iosp == 1);
}

TEST_F (PSOLIDParserTest, ParseEntry8)
{
    std::vector<std::string> Entry = { "PSOLID         8       1          BUBBLE       1       1SMECH" };
    BDF::divideBulkEntry (Entry, dividedEntryInfo);
    SG::Import::BDF::BULKDATA::ParsePSOLID (DB_SERVICEFACTORY, dividedEntryInfo);
    SG::DataStructure::FEM::PSOLIDData data;

    ASSERT_TRUE (DB_SERVICEFACTORY->get<IPSOLIDService> ()->FindById (8, data) == SG::DataStructure::Common::Status::STATUS_Success);

    ASSERT_TRUE (data.m_id == 8);
    ASSERT_TRUE (data.m_mId == 1);
    ASSERT_TRUE (data.m_mCId == 0);
    ASSERT_TRUE (data.m_in == 0);
    ASSERT_TRUE (data.m_stress == 1);
    ASSERT_TRUE (data.m_iosp == 1);
}